/**
 * (c)Copyright 2007 SinoCredit Co,Ltd. All Rights Reserved.  
 * BaseServiceImpl.java, 3:26:50 PM 
 * @author Ming.Shi
 * @since Oct 12, 2007
 * @version 1.0
 */
package com.sys.plat.service.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sys.plat.bean.PageBean;
import com.sys.plat.db.HibernateDao;

public class BaseServiceImpl implements BaseService {
	protected final Log log = LogFactory.getLog(getClass());
	protected HibernateDao dao;

	public HibernateDao getDao() {
		return dao;
	}

	public void setDao(HibernateDao dao) {
		this.dao = dao;
	}
	
	@SuppressWarnings("unchecked")
	public void page(PageBean pagebean) {
		String hql = pagebean.getHql();
		Map filtermap = pagebean.getFiltermap();
		pagebean.setTotalrow(this.dao.getTotalCount(hql, filtermap));
		if (pagebean.getPageno() > pagebean.getMaxpageno()) {
			pagebean.setPageno(pagebean.getMaxpageno());
		}
		pagebean.setRetList(this.dao.listWithPage(pagebean.getHql(), filtermap,
				pagebean.getGftSort(), pagebean.getPageno(), pagebean
						.getPagesize()));
	}
	
	
	public void delObject(Object obj) {
		this.dao.delObject(obj);

	}
	
	@SuppressWarnings("unchecked")
	public int delObject(String[] ids, Class clz) {
		if (ids == null || ids.length == 0)
			return 0;
		StringBuffer hql = new StringBuffer("DELETE FROM "+clz.getName()+" WHERE id in(''");
		List <String > list = new ArrayList<String>();
		for (String id : ids) {
			hql.append(",?");
			list.add(id);
		}
		hql.append(")");
		
		return this.dao.delete(hql.toString(),list);
	}
	
	@SuppressWarnings("unchecked")
	public int delObjectByColumn(String[] ids, Class clz, String column) {
		if (null == ids || 0 == ids.length)
			return 0;
		StringBuffer hql = new StringBuffer();
		hql.append("DELETE FROM ").append(clz.getName())
			.append(" WHERE ").append(column).append(" IN(''");
		List<String> list = new ArrayList<String>();
		for (String id : ids) {
			hql.append(",?");
			list.add(id);
		}
		hql.append(")");
		return this.dao.delete(hql.toString(),list);
	}
}
